package ErbeDesKoenigs;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import objects.*;

public class Playground extends JPanel implements ActionListener{
	private GridLayout settlementGl = new GridLayout(1, 6);
	private GridLayout soldiersGl = new GridLayout(1, 6);
	private GridLayout palaceGl = new GridLayout(1, 6);
	private GridLayout buildingsGl = new GridLayout(1, 6);
	
	private JPanel settlementsPanel;
	private JPanel soldiersPanel;
	private JPanel palacePanel;
	private JPanel buildingsPanel;
	
	public ArrayList<BuildingCard> openBuildingCards = new ArrayList<BuildingCard>();
	public ArrayList<PalaceCard> openPalaceCards = new ArrayList<PalaceCard>();
	public ArrayList<SettlementCard> openSettlementCards = new ArrayList<SettlementCard>();
	
	public ArrayList<BuildingCard> buildingCards = new ArrayList<BuildingCard>();
	public ArrayList<PalaceCard> palaceCards = new ArrayList<PalaceCard>();
	public ArrayList<SettlementCard> settlementCards = new ArrayList<SettlementCard>();
	public ArrayList<SoldierCard> soldierCards = new ArrayList<SoldierCard>();
	
	
	JButton panelSwitcherSettlement;
	JButton panelSwitcherSoldier;
	JButton panelSwitcherPalace;
	JButton panelSwitcherBuilding;
	int activePanel = 0;
	
	public Playground(){
		
		this.setSize(ErbeDesKoenigs.windowX, ErbeDesKoenigs.windowY/2);
		this.setLayout(null);

		panelSwitcherSettlement = new JButton("Switch");
		panelSwitcherSettlement.addActionListener(this);
		panelSwitcherSettlement.setActionCommand("panelSwitcher");

		panelSwitcherSoldier = new JButton("Switch");
		panelSwitcherSoldier.addActionListener(this);
		panelSwitcherSoldier.setActionCommand("panelSwitcher");

		panelSwitcherPalace = new JButton("Switch");
		panelSwitcherPalace.addActionListener(this);
		panelSwitcherPalace.setActionCommand("panelSwitcher");

		panelSwitcherBuilding = new JButton("Switch");
		panelSwitcherBuilding.addActionListener(this);
		panelSwitcherBuilding.setActionCommand("panelSwitcher");
		
		settlementsPanel = new JPanel();
		settlementsPanel.setSize(this.getSize());
		settlementsPanel.setLayout(settlementGl);
		settlementsPanel.setVisible(true);
		soldiersPanel = new JPanel();
		soldiersPanel.setSize(this.getSize());
		soldiersPanel.setLayout(soldiersGl);
		soldiersPanel.setVisible(false);
		buildingsPanel = new JPanel();
		buildingsPanel.setSize(this.getSize());
		buildingsPanel.setLayout(buildingsGl);
		buildingsPanel.setVisible(false);
		palacePanel = new JPanel();
		palacePanel.setSize(this.getSize());
		palacePanel.setLayout(palaceGl);
		palacePanel.setVisible(false);
		
		loadCards();
		refresh();
		
		this.add(buildingsPanel);
		this.add(settlementsPanel);
		this.add(soldiersPanel);
		this.add(palacePanel);
	}
	
	public void loadCards(){
		BuildingCard[] tmpBuildings = ErbeDesKoenigs.cardLoader.loadBuildings();
		for (int i = 0; i < tmpBuildings.length; i++) {
			buildingCards.add(tmpBuildings[i]);
		}
		PalaceCard[] tmpPalace = ErbeDesKoenigs.cardLoader.loadPalace();
		for (int j = 0; j < tmpPalace.length; j++) {
			palaceCards.add(tmpPalace[j]);
		}
		SettlementCard[] tmpSettlement = ErbeDesKoenigs.cardLoader.loadSettlement();
		for (int i = 0; i < tmpSettlement.length; i++) {
			settlementCards.add(tmpSettlement[i]);
		}
		SoldierCard[] tmpSoldiers = ErbeDesKoenigs.cardLoader.loadSoldiers();
		for (int i = 0; i < tmpSoldiers.length; i++) {
			soldierCards.add(tmpSoldiers[i]);
		}
	}
	
	
	
	public void refresh() {
		settlementsPanel = new JPanel();
		settlementsPanel.setSize(this.getSize());
		settlementsPanel.setLayout(settlementGl);
		settlementsPanel.setVisible(true);
		buildingsPanel = new JPanel();
		buildingsPanel.setSize(this.getSize());
		buildingsPanel.setLayout(buildingsGl);
		buildingsPanel.setVisible(false);
		palacePanel = new JPanel();
		palacePanel.setSize(this.getSize());
		palacePanel.setLayout(palaceGl);
		palacePanel.setVisible(false);
		soldiersPanel.setVisible(false);
		
		
		checkOpenCards();
		settlementsPanel.add(panelSwitcherSettlement, settlementGl);
		buildingsPanel.add(panelSwitcherBuilding, buildingsGl);
		palacePanel.add(panelSwitcherPalace, palaceGl);
		soldiersPanel.add(panelSwitcherSoldier, soldiersGl);
		for (int i = 0; i < openSettlementCards.size(); i++) {
			JButton cardButton = new JButton(openSettlementCards.get(i).cardName);
			cardButton.addActionListener(this);
			cardButton.setActionCommand("settlement"+i);
			settlementsPanel.add(cardButton, settlementGl);
		}
		
		this.repaint();
		ErbeDesKoenigs.myWin.repaint();
	}
	
	public void checkOpenCards(){
		/*while(openBuildingCards.size() < 5) {
			int random = (int)Math.round(Math.random()*buildingCards.size());
			openBuildingCards.add(buildingCards.get(random));
			buildingCards.remove(random);
		}*/
		while(openSettlementCards.size() < 5) {
			int random = (int)Math.round(Math.random()*settlementCards.size());
			openSettlementCards.add(settlementCards.get(random));
			settlementCards.remove(random);
		}
		System.out.println("Open Settlements: " 
		+ openSettlementCards.get(0).cardName + ", " 
		+ openSettlementCards.get(1).cardName + ", " 
		+ openSettlementCards.get(2).cardName + ", "
		+ openSettlementCards.get(3).cardName + ", "
		+ openSettlementCards.get(4).cardName);
	}
	
	public void attackSettlement(int index, PlayerInfo player){
		if(openSettlementCards.get(index).attackCard(player)){
			openSettlementCards.remove(index);
			checkOpenCards();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("panelSwitcher")){
			activePanel = (activePanel+1)%4;
			switch (activePanel) {
				case 0:
					settlementsPanel.setVisible(true);
					soldiersPanel.setVisible(false);
					buildingsPanel.setVisible(false);
					palacePanel.setVisible(false);
					break;
				case 1:
					settlementsPanel.setVisible(false);
					soldiersPanel.setVisible(true);
					buildingsPanel.setVisible(false);
					palacePanel.setVisible(false);
					break;
				case 2:
					settlementsPanel.setVisible(false);
					soldiersPanel.setVisible(false);
					buildingsPanel.setVisible(true);
					palacePanel.setVisible(false);
					break;
				case 3:
					settlementsPanel.setVisible(false);
					soldiersPanel.setVisible(false);
					buildingsPanel.setVisible(false);
					palacePanel.setVisible(true);
					break;
			default:
				break;
			}
		}
		if(e.getActionCommand().startsWith("settlement")){
			openSettlementCards.get(Integer.parseInt(e.getActionCommand().substring(10))).attackCard(ErbeDesKoenigs.players[ErbeDesKoenigs.activePlayer]);
		}
	}
}
